﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Web.Security;
using Gift.Model.ENUM;
using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.member
{
    public partial class member_add : BaseAdmin
    {
        readonly DALMember theMember = new DALMember();
        tbMember oMember;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddUser);
            }
        }

        void BuidForm()
        {
            if (Request.QueryString["mid"] != null)
            {
                oMember = theMember.GetModel(int.Parse(Request.QueryString["mid"]));
                this.txtLoginName.Text = oMember.memberlogin;
                this.txtUserName.Text = oMember.membername;
                this.txtEmail.Text = oMember.email;
                this.txtMobile.Text = oMember.phone;
                this.hdPassword.Text = oMember.memberpwd;
                this.txtLoginPassword.Required = false;
                this.txtConfirmPassword.Required = false;
            }
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["mid"] == null)
            {
                if (theMember.ExistsByLoginName(this.txtLoginName.Text.Trim()))
                {
                    ExtAspNet.Alert.Show("对不起，该会员名称已存在，请用别的会员名称！");
                    return;
                }
                oMember = new tbMember();
                oMember.email = this.txtEmail.Text.Trim();
                oMember.memberlogin = this.txtLoginName.Text.Trim();
                oMember.membername = this.txtUserName.Text.Trim();
                oMember.phone = this.txtMobile.Text.Trim();
                oMember.memberpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtLoginPassword.Text.Trim(), "MD5");
                oMember.registerTime = DateTime.Now;
                theMember.Add(oMember);
                ExtAspNet.Alert.Show("会员添加成功！", "", "openNewTab('UserList','/admin/member/member.aspx','会员列表','AddUser')");
            }
            else
            {
                oMember = theMember.GetModel(int.Parse(Request.QueryString["mid"]));
                oMember.email = this.txtEmail.Text.Trim();
                oMember.memberlogin = this.txtLoginName.Text.Trim();
                oMember.membername = this.txtUserName.Text.Trim();
                oMember.phone = this.txtMobile.Text.Trim();
                if (this.txtLoginPassword.Text.Trim() != "")
                {
                    oMember.memberpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtLoginPassword.Text.Trim(), "MD5");
                }

                theMember.Update(oMember);
                ExtAspNet.Alert.Show("会员修改成功！", "", "openNewTab('UserList','/admin/member/member.aspx','会员列表','MemberEdit')");
            }
            
        }


    }
}